package org.example.bipredicate;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

public class Java8BiPredicate2 {
    public static void main(String[] args) {
        List<Domain> domains = Arrays.asList(new Domain("google.com", 1),
                new Domain("i-am-spammer.com", 10),
                new Domain("mkyong.com", 0),
                new Domain("microsoft.com", 2));
        BiPredicate<String,Integer> bi = (s, integer) -> s.equalsIgnoreCase("google.com") || integer.equals(0);
        List<Domain> result = filterBadDomain(domains,bi);
        System.err.println(JSONObject.toJSONString(result,true));

        List<Domain> result2 = domains.stream().filter(d -> d.getScore().equals(0)).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(result2,true));
        List<Domain> result3 = filterBadDomain(domains,(x,y) -> y.equals(0));
        System.err.println(JSONObject.toJSONString(result3,true));
        List<Domain> result4 = filterBadDomain(domains,(x,y) -> x.startsWith("i") && y > 5);
        System.err.println(JSONObject.toJSONString(result4));
    }

    public static <T extends Domain> List<T> filterBadDomain(List<T> list, BiPredicate<String, Integer> biPredicate){
        return list.stream().filter(x -> biPredicate.test(x.getName(), x.getScore())).collect(Collectors.toList());
    }
}
class Domain{
    String name;
    Integer score;

    public Domain(String name, Integer score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }
}
